new Vue({
    el: '#app',
    mixins: [Query],//组件配置文件，路径/utils/components.js
    data() {
        return {
            form: {//查询筛选
                value: '',
                value2: '-', // 状态：'-'全部、'0'生效中、'9'待审核、'2'拒绝
                PageNumber: 1, //页码
                RowAmount: 20,
                // Condition（三参）：关键词,状态,创建人（平台传'%%'，企业传当前用户）
                Condition: (function(){
                    const isAdmin = getCookie('enterprise') === '1';
                    const creator = isAdmin ? '%%' : getCookie('user');
                    return utf8ToBase64(`%%,%%,${creator}`);
                })(),
                // ExtensionCondition：仅企业范围过滤
                ExtensionCondition: (function(){
                    const ent = getCookie('enterprise');
                    if (!ent || ent === '1') return utf8ToBase64('');
                    return utf8ToBase64(`AND acquisition_unitprice.Enterprise_Info_SerialNumber='${ent}'`);
                })(),
            },
            isAdmin: getCookie('enterprise') === '1',
        }
    },
    mounted() {
        /**
         * 组件配置文件中自动执行方法
         * 1.this.getColumn(); //查询表头数据
         * 2.this.getList(this.form); //查询表格数据
         * 3.this.getTreeData();//查询树
         */
        
    },
    methods: {
        // 仅应用企业范围到 ExtensionCondition（状态通过 Condition 第二参数传递）
        applyEnterpriseFilter() {
            const ent = getCookie('enterprise');
            const base = (!ent || ent === '1') ? '' : `AND acquisition_unitprice.Enterprise_Info_SerialNumber='${ent}'`;
            this.form.ExtensionCondition = utf8ToBase64(base);
        },
        handleSerach() { //筛选
            this.form.PageNumber = 1;
            // Condition：关键词,状态,创建人
            const creator = this.isAdmin ? '%%' : getCookie('user');
            const val = this.form.value2;
            const status = (val === '-' || val === '' || val == null) ? '%%' : val;
            this.form.Condition = utf8ToBase64(`%${this.form.value}%,${status},${creator}`);
            this.applyEnterpriseFilter();
            this.getList(this.form);
        },
        // 状态切换
        handleTabsClick() {
            // 更新 Condition 的第二参数为状态，不改变关键词；重置到第一页
            this.form.PageNumber = 1;
            const creator = this.isAdmin ? '%%' : getCookie('user');
            const val = this.form.value2;
            const status = (val === '-' || val === '' || val == null) ? '%%' : val;
            this.form.Condition = utf8ToBase64(`%${this.form.value}%,${status},${creator}`);
            this.applyEnterpriseFilter();
            this.getList(this.form);
        },
        handleCommand(cmd, row) { // 下拉操作分发
            if (cmd === 'review') {
                const { primaryKey } = $('#table').data('options');
                const id = row[primaryKey];
                // 审核入口：显示通过/拒绝按钮
                window.location.href = `./review_detail.html?serial=${encodeURIComponent(id)}&canReview=1`;
                return;
            }
            if (typeof this[cmd] === 'function') {
                this[cmd](row);
            }
        },
        handleEdit(row) { // 详情入口：只读查看审核详情
            try {
                const { primaryKey } = $('#table').data('options');
                const id = row[primaryKey];
                window.location.href = `./review_detail.html?serial=${encodeURIComponent(id)}`;
            } catch (error) {
                console.error(error);
                this.$message({ message: '系统错误，请联系开发者!', type: 'error' });
            }
        },
        resubmit(row) { // 修改并重新提交：跳转到编辑模式
            try {
                const { primaryKey } = $('#table').data('options');
                const id = row[primaryKey];
                window.location.href = `./add.html?action=Modify&nodeKey=${encodeURIComponent(id)}`;
            } catch (error) {
                console.error(error);
                this.$message({ message: '系统错误，请联系开发者!', type: 'error' });
            }
        },
        
    }
})